import storage from 'store'
import { login, getInfo } from '@/api/login'
import { welcome } from '@/utils/util'
import { ACCESS_TOKEN, REFRESH_TOKEN } from '@/store/mutation-types'
import qs from 'qs'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    roled:[],
    roleid: '',
    info: {},
    userid: '',
    departmentID: '',
    loginName: '',
    ZHLB:'0',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ROLED: (state, roled) => {
      state.roled = roled
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_ROLEID: (state, roleid) => {
      state.roleid = roleid
    },
    SET_USERID: (state, userid) => {
      state.userid = userid
    },
    SET_DEPARTMENTID: (state, departmentID) => {
      state.departmentID = departmentID
    },
    SET_LOGINNAME: (state, loginName) => {
      state.loginName = loginName
    },
    SET_ZHLB: (state, ZHLB) => {
      state.ZHLB = ZHLB
    },
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
    console.log('userInfo',userInfo);
      return new Promise((resolve, reject) => {
        login(qs.stringify(userInfo)).then(response => {
          if (response.access_token) {
            const result = response
            storage.set(ACCESS_TOKEN, result.token_type + ' ' + result.access_token, 7 * 24 * 60 * 60 * 1000)
            storage.set(REFRESH_TOKEN, result.refresh_token)
            commit('SET_TOKEN', result.access_token)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const result = response.data
          if (result.roleList && result.roleList.length > 0) {
            commit('SET_ROLES', result.roleList)
            commit('SET_ZHLB', result.ZHLB)
            commit('SET_ROLED', result.roleDate)
            commit('SET_INFO', result)
            commit('SET_NAME', { name: result.UserName, welcome: welcome() })
            commit('SET_AVATAR', result.UploadImg)
            commit('SET_USERID', result.UserID)
            commit('SET_DEPARTMENTID', result.DeptID)
            commit('SET_LOGINNAME', result.LoginName)
            resolve(response)
          } else {
            reject(new Error('getInfo: roles must be a non-null array !'))
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      storage.remove(ACCESS_TOKEN)
      storage.remove(REFRESH_TOKEN)
      // return new Promise((resolve, reject) => {
      //   logout().then(() => {
      //     commit('SET_TOKEN', '')
      //     commit('SET_ROLES', [])
      //     storage.remove(ACCESS_TOKEN)
      //     storage.remove(REFRESH_TOKEN)
      //     resolve()
      //   }).catch((err) => {
      //     console.log('logout fail:', err)
      //     reject(err)
      //   })
      // })
    }

  }
}

export default user
